<?php
/**
 * Created by PhpStorm.
 * User: yui
 * Date: 2017/3/31
 * Time: 0:51
 */

namespace api\controllers;

use common\controllers\ApiCommonCtrl;
use common\services\AreaSve;
use common\services\PersonnelClassSve;
use common\services\WorkSve;
use Yii;
use common\services\MemberSve;

class MemberController extends ApiCommonCtrl
{
    /**
     * 用户信息
     * @return mixed
     */
    public function actionInfo()
    {
        $this->checkLogin();
        //实例化会员服务类
        $MemberSve = new MemberSve();
        //登录
        $rs = $MemberSve->getInfo(['member_id' => $this->TokenObj->member_id]);
        if ($rs['code'] === 0) {
            unset($rs['data']['Obj']->password, $rs['data']['Obj']->pay_password, $rs['data']['Obj']->password_reset_token, $rs['data']['Obj']->auth_key);
        }
        return $rs;
    }

    /**
     * 登陆
     */
    public function actionLogin()
    {
        $post = Yii::$app->request->post();
        //实例化会员服务类
        $MemberSve = new MemberSve();
        //登录
        return $MemberSve->login($post);
    }

    /**
     * 短信登陆
     * @return array
     */
    public function actionLoginBySms()
    {
        $post = Yii::$app->request->post();
        //实例化会员服务类
        $MemberSve = new MemberSve();
        //登录
        return $MemberSve->loginBySms($post);
    }

    /**
     * 注册
     * @return array
     */
    public function actionRegister()
    {
        $post = Yii::$app->request->post();
        $post['nickname'] = isset($post['mobile']) ? $post['mobile'] : '';
        $post['province_id'] = isset($post['province_id']) ? $post['province_id'] : 1;
        $post['city_id'] = isset($post['city_id']) ? $post['city_id'] : 36;
        $post['pca_info'] = isset($post['pca_info']) ? $post['pca_info'] : '北京 北京市';
        //实例化会员服务类
        $MemberSve = new MemberSve();
        //登录
        return $MemberSve->add($post);
    }

    /**
     * 更新用户信息
     * @return array
     */
    public function actionUpdate()
    {
        $this->checkLogin();
        $post = Yii::$app->request->post();
        $post['member_id'] = $this->TokenObj->member_id;
        // 添加pca_info UPDATE BY:lihouse
        if (isset($post['province_id']) && isset($post['city_id'])) {
            $pcaInfo = '';
            $rs = (new AreaSve())->getInfo(['area_id' => $post['province_id']]);
            if ($rs['code'] === 0) {
                $pcaInfo = $pcaInfo . $rs['data']['Obj']['area_name'];
            }
            $rs = (new AreaSve())->getInfo(['area_id' => $post['city_id']]);
            if ($rs['code'] === 0) {
                $pcaInfo = $pcaInfo . ' ' . $rs['data']['Obj']['area_name'];
            }
            $post['pca_info'] = $pcaInfo;
        }
        //实例化会员服务类
        $MemberSve = new MemberSve();
        //登录
        return $MemberSve->update($post, 'self_update');
    }

    /**
     * 登出
     * @return mixed
     */
    public function actionLogout()
    {
        $this->checkLogin();
        //实例化会员服务类
        $MemberSve = new MemberSve();
        //退出
        return $MemberSve->logout($this->TokenObj->token);
    }


    /**
     * 修改密码
     */
    public function actionResetPwd()
    {
        $this->checkLogin();
        $post = Yii::$app->request->post();
        //实例化会员服务类
        $MemberSve = new MemberSve();
        //退出
        $Member = $this->TokenObj;
        return $MemberSve->resetPwd($post,$Member);
    }


    /**
     * 获取会员自己的下级
     * @return array
     */
    public function actionMyRole()
    {
        //检测登录
        self::checkLogin();
        $Member = $this->TokenObj;
        $re = (new MemberSve())->getRoleList(['or',['=','parent_role',$Member->role],['=','ancestors_role',$Member->role]]);

        $temArr = [];
        $PersonnelClassSve = new PersonnelClassSve();
        $WorkSve = new WorkSve();
        foreach ($re['data']['list'] as $key=>$data){
            $tem=[];

            $tem['member_id'] = $data['member_id'];
            $tem['mobile'] = $data['mobile'];
            $tem['nickname'] = $data['nickname'];
            $tem['office'] = $data['office'];
            $tem['position'] = $data['position'];
            $tem['introduction'] = $data['introduction'];
            $tem['avatar'] = $data['avatar']?HTTP_DOMAIN_ASSETS . '/'.$data['avatar']:'' ;
            $tem['sex'] = $data['sex'];
            $tem['role'] = $data['role'];
            $pclass = $PersonnelClassSve->getInfo(['personnel_class_id'=>$data['role']]);
            $tem['role_name'] = $pclass['data']['Obj']->class_name;



            //获取现在去向
            $now = time();
            $today = strtotime(date('Y-m-d'));
            $workInfo = $WorkSve->getWork(['and',['<','start_time',$now],['>','end_time',$now],['>','start_time',$today]],'start_time DESC,end_time ASC,work_id DESC');
            if($workInfo){
                $tem['start_time'] = $workInfo['start_time'];
                $tem['end_time'] = $workInfo['end_time'];
                $tem['work_class_name'] = $workInfo['work_class_name'];
            }else{
                $tem['start_time'] = '--';
                $tem['end_time'] = '--';
                $tem['work_class_name'] = '未填写';
            }


            array_push($temArr,$tem);
        }
        $re['data']['list'] = $temArr;
        return $re;
    }




}